package com.auoem.radio.ui.other;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.auoem.radio.R;
import com.auoem.radio.base.BaseActivity;
import com.auoem.radio.common.adapter.BindingAdapter;
import com.auoem.radio.databinding.ActivityTestDbBinding;
import com.auoem.radio.ui.msg.MsgListActivity;
import com.auctus.am.framework.constants.AmConstants;
import com.auctus.am.framework.imp.HlThreadPoolManager;
import com.auctus.am.framework.imp.MsgControl;
import com.auctus.am.framework.imp.RadioManager;
import com.auctus.am.framework.roomdb.entity.AmMsg;
import com.auctus.am.framework.roomdb.entity.AmTalk;
import com.auctus.am.framework.roomdb.repository.AmMsgRep;
import com.auctus.am.framework.utils.LogM;

import java.util.ArrayList;
import java.util.List;
import java.util.function.IntConsumer;

public class TestDbActivity extends BaseActivity implements View.OnClickListener, IntConsumer, MsgControl.MsgListener {

    private final static String TAG = "TestDbActivity";
    ActivityTestDbBinding mBinding;

    private int DEFAULT_SND_ID = 500;

    private List<AmTalk> talkList;
    private BindingAdapter<AmTalk> talkAdapter;

    private MsgControl msgControl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityTestDbBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        initData();
        initView();
    }

    @Override
    protected void onDestroy() {
        msgControl.setMsgListener(null);
        super.onDestroy();
    }


    @Override
    protected void onStart() {
        super.onStart();
        requestAndUpdateTalkList();
    }

    private void initView() {
//        talkList = AmMsgRep.getInstance().getAllTalk();
        if (talkList == null) {
            talkList = new ArrayList<>();
        }
        talkAdapter = new BindingAdapter<>(this, talkList, R.layout.adapter_item_talk);
        talkAdapter.setOnItemClickListener(this);
        mBinding.lvTalk.setLayoutManager(new LinearLayoutManager(this));
        mBinding.lvTalk.setAdapter(talkAdapter);
    }

    private void initData() {
//        AmMsgRep.getInstance().init(HlApplication.getApplication());
        msgControl = RadioManager.getInstance().getMsgControl();
        msgControl.setMsgListener(this);
        AmMsgRep.getInstance().init(this);
    }


    @Override
    public void onClick(View v) {
        if (v == mBinding.btInitDb) {
            initDb();
        } else if (v == mBinding.btClear) {
            clearDb();
        } else if (v == mBinding.btClearDistinct) {
            clearDistinct();
        } else if (v == mBinding.btInsert) {
            insertTalkItem();
        } else if (v == mBinding.btListTalk) {
            listTalk();
        } else if (v == mBinding.btListTalkItems) {
            listTalkItems();
        }
    }

    private void initDb() {

    }

    private void clearDb() {
        AmMsgRep.getInstance().clsTalkAndMsg();
    }

    private void clearDistinct() {
        AmMsgRep.getInstance().clsDistinct();
    }

    private void insertTalkItem() {
        int idRev = Integer.parseInt(mBinding.etRev.getText().toString().trim());
        int idSnd = Integer.parseInt(mBinding.etSnd.getText().toString().trim());
        boolean isSnd = idSnd == DEFAULT_SND_ID;
        String msg = mBinding.etMsg.getText().toString().trim();
        AmMsg amMsg = new AmMsg();
        amMsg.setMsgMuti(idSnd, idRev, msg, true, !isSnd);
        msgControl.doOnRevMsg(amMsg);
    }

    private void listTalk() {
//        talkList.clear();
//        talkList.addAll(Objects.requireNonNull(AmMsgRep.getInstance().getAllTalk()));
//        talkAdapter.notifyDataSetChanged();
        requestAndUpdateTalkList();
    }

    private void listTalkItems() {

    }


    @Override
    public void accept(int value) {
        AmTalk talk = talkList.get(value);
        if (talk == null) {
            //异常
            return;
        }
        zero(talk.getTarId());
        Intent intent = new Intent(this, MsgListActivity.class);
        intent.putExtra(AmConstants.KEY_MSG_TAR_ID, talk.getTarId());
        startActivity(intent);
    }

    @Override
    public void onNewMsg(AmTalk talk, AmMsg msg) {
        LogM.d(TAG, "onNewMsg: " + talk.toString());
        requestAndUpdateTalkList();
    }

    private void requestAndUpdateTalkList() {
        HlThreadPoolManager.getInstance().execut("requestTalkDb", new Runnable() {
            @Override
            public void run() {
                List<AmTalk> newTalks = msgControl.getTalks();
                LogM.d(TAG, "requestDb : " + (newTalks == null ? "empty" : newTalks.size()));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        talkList.clear();
                        talkList.addAll(newTalks);
                        talkAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    private void zero(int tarId) {
        HlThreadPoolManager.getInstance().execut("requestTalkDb", new Runnable() {
            @Override
            public void run() {
                msgControl.zeroTalkNewCount(tarId);
            }
        });
    }
}
